Skip to content

chore(agents): run the push check in Pi - #136

Merged
scottlovegrove merged 3 commits into
scottl/agent-push-checkfrom
scottl/agent-guard-pi
Nov 27, 2026
Merged

scottlovegrove merged 3 commits into
scottl/agent-push-checkfrom
scottl/agent-guard-pi

Conversation

@scottlovegrove

@scottlovegrove scottlovegrove commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Runs the push check from #135 in Pi as well, ported from todoist/todoist-cli#673. Pi has no hook files, so .pi/extensions/agent-guard.ts listens to Pi's tool_call event: before a bash call that could push, it runs the same .agents/scripts/guard.ts the Claude Code and Codex hooks call, with the same JSON, and blocks the call with the guard's reason when it says no.

It behaves like the other agents' hooks: DISABLE_AGENT_GUARD=1 turns it off, a checkout without the guard is skipped without starting anything, and a guard that fails, times out or prints something odd allows the call with a note. Pi blocks a tool call whose handler throws, so the handler catches its own errors and allows the call too. Doistbot's review here found that a guard that can't load at all, such as one with a syntax error, was read as having nothing to say, so no note appeared; todoist-cli's extension does the same. It now gives a note with the exit code and the guard's stderr, and the tests for the calls it skips check that it never starts the guard. Pi only loads project extensions once the project is trusted, and only from the folder it starts in, so docs/agents.md says to start Pi at the repository root.

One difference from todoist-cli: there, .pi had to be added to the lint paths of npm run check and verify's lint step. Here npm run lint:check and verify already run oxlint . and oxfmt over the whole repository, and both catch problems in .pi (I checked with an unused variable and a badly formatted line in the extension), so package.json and the verify steps are unchanged. The tooling tsconfig now includes .pi/**/* so the extension is type-checked, and .pi/ joins the dev-only paths the package-contents test keeps out of the npm package. The tests are in .agents/scripts/pi-extension.test.ts, since Pi would load a test file in .pi/extensions/ as another extension.

Tried with Pi 0.85.1 (pi -p --approve) at the top of this stack, in a throwaway clone pushing to a local bare repository: the unverified push was blocked with the guard's reason, and after npm run verify the same push went through. The JSON event stream showed the blocked call, and the bare repository's ref matched the pushed commit.

@doistbot doistbot left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean port of the push check to Pi: the extension reuses .agents/scripts/guard.ts with the same payload and fail-open behaviour as the Claude Code and Codex hooks, catches its own errors so Pi can't turn one into a block, and is consistently wired into the tooling tsconfig, tests, and docs.

Few things worth tightening:

  • Guard startup failures are silently allowed: if guard.ts exits unsuccessfully (e.g. a syntax error), the handler converts the empty stdout to {} and discards stderr, so the push goes through without the promised note. Report the failed exit/signal as a diagnostic while keeping successful-but-empty output silent, and add a test that corrupts guard.ts itself.
  • The tests can't distinguish "skipped" from "started and failed": deleting the existsSync(GUARD) check or the toolName/MIGHT_PUSH early returns still passes, since every failure path resolves to the same undefined-with-no-notes shape. Pin the skip cases with a sentinel (e.g. a log file that must never be created), mirroring what hook-wiring.test.ts does with stub git/node scripts.
  • Minor test cleanup: the new test hand-rolls file creation that TempRepo.write already covers.

I also included a few optional follow-up notes in the details below.

Optional follow-up notes (4)
  • P3 docs/agents.md:68: The per-agent enumeration drops Cursor: "Claude Code and Codex from their hook configs, Pi from a small extension". Cursor is still wired (it runs the Claude Code hooks, as the table two paragraphs down says), so the rewritten sentence reads as if only three agents are covered. Add Cursor to the list, e.g. "Claude Code and Codex from their hook configs, Cursor through the Claude Code hooks, Pi from a small extension".
  • P3 .agents/scripts/pi-extension.test.ts:54: mkdirSync + writeFileSync here re-implement TempRepo.write, which already creates parent folders and is used a few lines up in setUp. Taking the target TempRepo in drive instead of root (defaulting to the module-level repo) would let this be target.write('.tmp/event.json', JSON.stringify(event)), and drops the two node:fs imports.
  • P3 .agents/scripts/pi-extension.test.ts:156: The test suite covers every other fail-open branch of runGuard — failed to run, printed no JSON, exited before reading stdin — but not the timeout. The timeout is now the extension's own responsibility (Claude Code and Codex get their 30s timeout from their hook configs, e.g. .claude/settings.json's timeout: 30; Pi has no such setting), and docs/agents.md and the PR description both list "times out" as designed behavior. If the timer's finish call regressed, the handler would await forever and hang every push-like tool call in Pi, and no existing test would notice. Since 30s can't be waited out, consider letting the test set a short timeout — e.g. have runGuard read an override like AGENT_GUARD_TIMEOUT_MS from the environment (the isolated test env already manages agent-guard env vars) — and add one test asserting a hung guard produces the "timed out, so it allowed the action" note and lets the call through.
  • P3 .pi/extensions/agent-guard.ts:71: The timeout note drops the guard's stderr, which the handler has been collecting the whole time. The no-JSON path at line 83 includes stderr.trim() in its note so a broken guard explains itself, but a guard that hangs produces only "timed out, so it allowed the action" — the least actionable of the failure modes. Include the collected stderr in the timeout message the same way, so a hung guard (e.g. waiting on git) says what it printed before stalling.

Share Feedback • Review Logs • Trace

Comment thread .pi/extensions/agent-guard.ts Resolved
Comment thread .agents/scripts/pi-extension.test.ts Resolved
@scottlovegrove scottlovegrove added the 🚢 Ship PR requires passing checks before merge, but no independent human review. label Nov 27, 2026
Runs the push check in Pi as well, ported from todoist/todoist-cli#673.
Pi has no hook files, so .pi/extensions/agent-guard.ts listens to Pi's
tool_call event: before a bash call that could push, it runs the same
.agents/scripts/guard.ts the Claude Code and Codex hooks call, with the
same JSON, and blocks the call with the guard's reason when it says no.

It behaves like the other agents' hooks: DISABLE_AGENT_GUARD=1 turns it
off, a checkout without the guard is skipped without starting anything,
and a guard that fails, times out or prints something odd allows the
call with a note. Pi blocks a tool call whose handler throws, so the
handler catches its own errors and allows the call too.

The extension is type-checked with the rest of the agent tooling (the
tooling tsconfig now includes .pi), and oxlint and oxfmt already cover
.pi, since both run over the whole repository. Its tests are in
.agents/scripts/pi-extension.test.ts, since Pi would load a test file in
.pi/extensions/ as another extension.
When guard.ts itself failed to load, such as with a syntax error, node
exited with nothing on stdout and the extension read that as "nothing
to say", so the push went through without the note the other agents
show. A guard that exits unsuccessfully now gives a note with its exit
code or signal and what it printed on stderr, and a timeout note
carries the stderr too.

The tests for the calls the extension skips (other tools, commands
that can't push, DISABLE_AGENT_GUARD=1, a checkout without the guard)
now put a `node` on PATH that records each start, so they fail if the
extension starts the guard at all, rather than passing on a guard that
ran and said nothing. The test driver writes its event file with
TempRepo.write.
The overview listed Claude Code, Codex and Pi; Cursor runs the Claude
Code hooks, as the table below it says.
@scottlovegrove
scottlovegrove merged commit 4f23868 into main Nov 27, 2026
11 checks passed
@scottlovegrove
scottlovegrove deleted the scottl/agent-guard-pi branch November 27, 2026 14:35
scottlovegrove added a commit to todoist/todoist-mcp that referenced this pull request Nov 27, 2026
# Pull Request

## Short description

Part 2 of 3, on top of #784. Runs the push check in Pi, ported from
todoist/todoist-cli#673. Pi has no hook files, so
`.pi/extensions/agent-guard.ts` listens to Pi's `tool_call` event:
before a `bash` call that could push, it runs the same
`.agents/scripts/guard.ts` with the same JSON, and blocks the call with
the guard's reason when it says no.

Pi only loads project extensions once the project is trusted (`pi
--approve`, or the prompt on first run), and only from the folder it
starts in, so AGENTS.md and docs/agents.md say to start Pi at the
repository root. `.pi` is now in the lint paths (`format:check`,
`format:fix`, `check` and verify's lint step) and in the tooling
tsconfig as `../../.pi/**/*` (a bare `../../.pi` includes nothing; a
test asks tsc what it reads). `.pi/` is in the package-contents test's
dev-only list and the instructions test's rooted paths. The tests live
in `.agents/scripts/pi-extension.test.ts`, since Pi would load a test
file in `.pi/extensions/` as another extension.

Fixes from the other ports, each with a test that fails without it:

- A guard that crashes or can't start (a syntax error makes Node exit
non-zero with only stderr) used to resolve to `{}` and allow the push
without a word. It now allows it with a note carrying the exit code or
signal and stderr (all three ports; the wording follows comms-cli, which
also puts stderr in the timeout note).
- A `ui.notify` that throws could turn a denial into an allow, since the
handler's catch allows. The block is worked out first and a failed note
falls back to stderr (outline-cli, todoist/outline-cli#194).
- The skip tests put a `node` on `PATH` that records each start, so they
tell "skipped" from "started and failed" (comms-cli,
todoist/comms-cli#136).

Tried with Pi 0.85.1 (`pi -p --approve --mode json`) in a throwaway
clone pushing to a local bare repository: the JSON event stream shows
the unverified push blocked with the guard's reason, `npm run verify`
passing, and the same push then going through; the bare repo has the
branch.

## PR Checklist

- [x] Added tests for bugs / new features
- [x] Updated docs (README, etc.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🚢 Ship PR requires passing checks before merge, but no independent human review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants